package net.flydy.ywxcx.wxcloudrun.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;


@Configuration(proxyBeanMethods = false)
public class MvcConfig implements WebMvcConfigurer  {

    @Autowired
    private MerchantLoginInterceptor merchantLoginInterceptor;

//    @Bean
//    public Jackson2ObjectMapperBuilderCustomizer jacksnoCustomizer() {
//        return builder -> {
//            JavaTimeModule timeModule = new JavaTimeModule();
//            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//            // Deserializer
//            LocalDateTimeDeserializer localDateTimeDeserializer = new
//                    LocalDateTimeDeserializer(dateTimeFormatter);
//            timeModule.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);
//            LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(dateTimeFormatter);
//            timeModule.addSerializer(LocalDateTime.class, localDateTimeSerializer);
//            builder.modules(timeModule);
//            builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, SerializationFeature.FAIL_ON_EMPTY_BEANS)
//                    .serializationInclusion(JsonInclude.Include.NON_NULL)
//                    .simpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        };
//    }

//    @Bean
//    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
//        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
//        JavaTimeModule timeModule = new JavaTimeModule();
//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        // Deserializer
//        LocalDateTimeDeserializer localDateTimeDeserializer = new
//                LocalDateTimeDeserializer(dateTimeFormatter);
//        timeModule.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);
//        LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(dateTimeFormatter);
//        timeModule.addSerializer(LocalDateTime.class, localDateTimeSerializer);
//        builder.modules(timeModule);
//        builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, SerializationFeature.FAIL_ON_EMPTY_BEANS)
//                .serializationInclusion(JsonInclude.Include.NON_NULL)
//                .simpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        builder.failOnEmptyBeans(false);
//        return builder;
//    }

//    @Bean
//    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
//        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
////        objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
//        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
////        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
//        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//        jsonConverter.setObjectMapper(objectMapper);
//        return jsonConverter;
//    }
//
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        converters.add(customJackson2HttpMessageConverter());
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.
                addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);

        registry.addResourceHandler("/static/**") // 表示文件路径
                .addResourceLocations("classpath:/static/"); // 表示要开放的资源
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/swagger-ui/")
                .setViewName("forward:/swagger-ui/index.html");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //后台拦截器
        InterceptorRegistration registration = registry.addInterceptor(new AdminLoginInterceptor());
        registration.addPathPatterns("/admin/**"); //所有路径都被拦截
        registration.excludePathPatterns(    //添加不拦截路径
                "/admin/doLogin",           //登录路径
                "/**/*.html",                 //html静态资源
                "/**/*.js",                  //js静态资源
                "/**/*.css"                  //css静态资源
        );

        registration = registry.addInterceptor(merchantLoginInterceptor);
        registration.addPathPatterns("/merchant/**"); //所有路径都被拦截
        registration.excludePathPatterns(    //添加不拦截路径
                "/merchant/merchantWXLogin",           //登录路径
                "/**/*.html",                 //html静态资源
                "/**/*.js",                  //js静态资源
                "/**/*.css"                  //css静态资源
        );
    }
}